package com.birdcage.gamemanagers;

import android.util.Log;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;

public class GraphicsElement
{
    ///Private member.
    private GameObject m_parent = null;   // if it is null, this element will be handled manually.
    private boolean m_active = true;  
    private Vector2 m_pos = new Vector2(0, 0);
    private TextureRegion m_unit = null;
    private int m_width;
    private int m_height;
    private Vector2 m_scale = new Vector2(1,1);
    private Vector2 m_origin = new Vector2(0, 0);
    private float m_rotation = 0;
    private String m_info;
    private boolean m_updateByGraphicsManager = false; // don't change it anywhere except the construct method.
    
    ///Construct
    //For game object.
    public GraphicsElement(GameObject parent, Texture image)
    {
        NormalInit(parent, image);
    }
    
    //For any other usage.
    public GraphicsElement(Texture image, String info)
    {
        m_unit = new TextureRegion(image);
        SetInfo(info);
    }
    
    public GraphicsElement(GameObject parent, Texture image, float originX, float originY)
    {
        NormalInit(parent, image);
        m_origin = new Vector2(originX, originY);     
    }
    
    public GraphicsElement(GameObject parent, Texture image, Vector2 origin)
    {
        NormalInit(parent, image);
        m_origin = origin;   
    }
    
    ///Public Method
    public boolean GetActive()
    {
        boolean result = m_active;
        if (m_parent != null && m_active)
        {
            result = m_parent.GetActive();
        }
        return result;
    }
    
    public void Update(SpriteBatch renderDev)
    {
        if (GetActive())
        {
            if (m_parent != null)
            {
                Vector2 worldPos = new Vector2();
                worldPos.set(m_parent.GetWorldPos());       //initial with parent's world position
                worldPos.add(m_pos);                        //offset by graphics position
                
                Vector2 scale = new Vector2();
                scale.set(m_parent.GetScale());
                scale.x *= m_scale.x;
                scale.y *= m_scale.y;
                
                float rotation = m_rotation + m_parent.GetWorldRotation();
                
                renderDev.draw( m_unit, worldPos.x, worldPos.y, m_origin.x, m_origin.y,GetWidth(), GetHeight(), scale.x , scale.y, rotation);
                
                String info = String.format("x: %f, y:%f, width:%d, height:%d, rotation:%f\n", worldPos.x, worldPos.y, GetWidth(), GetHeight(),rotation);
                Log.i("RenderPos_P", info);
            }
            else
            {
                renderDev.draw(m_unit.getTexture(), GetPos().x, GetPos().y, (int)m_origin.x, (int)m_origin.y, GetWidth(), GetHeight());

                String info = String.format("x: %f, y:%f\n", GetPos().x, GetPos().y);
                Log.i("RenderPos", info);
            }
        } 
    }

    public void SetPos(Vector2 pos)
    {
        this.m_pos = pos;
    }

    public Vector2 GetPos()
    {
        return m_pos;
    }
    
    public void SetOrigin(Vector2 origin) 
    {
        this.m_origin = origin;
    }
    
    public void SetOrigin(float x, float y)
    {
        this.m_origin = new Vector2(x, y);
    }
    
    public Vector2 GetOrigin() 
    {
        return m_origin;
    }

    public void SetWidth(int m_width)
    {
        this.m_width = m_width;
    }

    public int GetWidth()
    {
        return m_width;
    }

    public void SetHeight(int height)
    {
        this.m_height = height;
    }

    public int GetHeight()
    {
        return m_height;
    }

    public void SetInfo(String info)
    {
        this.m_info = m_info;
    }

    public String GetInfo()
    {
        return m_info;
    }

    public boolean IsUpdateByGraphicsManager()
    {
        return m_updateByGraphicsManager;
    }
    
    //private method
    private void NormalInit(GameObject parent, Texture image) 
    {
        m_parent = parent;
        m_unit = new TextureRegion(image);
        m_width = image.getWidth();
        m_height = image.getHeight();
        GraphicsManager.GetInstance().AddElement(this);
        m_updateByGraphicsManager = true;
    }
}
